package com.beta.nfc_beta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MenuActivity extends Activity {
	private ListView lv;
	private SimpleAdapter adapter;
	
	public static final int BANK_MODE=0;
	public static final int CREDIT_MODE=1;
	
	private static final String tag="Menu";
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		lv=(ListView)this.findViewById(R.id.list_of_cards);
		adapter=new SimpleAdapter(this,getData(),R.layout.vlist,new String[]{"Image","Number","Name"},
				new int[]{R.id.card_image,R.id.content_number,R.id.content_name});
		MyViewBinder viewBinder=new MyViewBinder();
		adapter.setViewBinder(viewBinder);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if(position==adapter.getCount()-1){
					final Intent add_card=new Intent(MenuActivity.this,ChooseCardActivity.class);
					 Builder builder=new AlertDialog.Builder(MenuActivity.this);
					    builder.setTitle("Select Card Type");
					    builder.setItems(new String[] { "Bank Card", "Credit Card" },
					    		new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if(which==0){
									add_card.putExtra("Mode",BANK_MODE);
								}else
									add_card.putExtra("Mode", CREDIT_MODE);
								startActivity(add_card);
							}
					    	
					    });		 
					    AlertDialog dialog=builder.create();
					    dialog.show();
				}else{
					Toast.makeText(getApplicationContext(), "card number:62202011568888", 
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int pos, long id) {
				// TODO Auto-generated method stub
				if(pos!=adapter.getCount()-1){
					 Builder builder=new AlertDialog.Builder(MenuActivity.this);
					    builder.setTitle("Card Options");
					    builder.setItems(new String[] { "View Details", "Delete Card" },
					    		new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if(which==0){
									//Details of Card
									Toast.makeText(getApplicationContext(), "Datails", Toast.LENGTH_SHORT)
									.show();
								}else{
									//Delete from Database
									Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT)
									.show();
								}									
							}
					    	
					    });		 
					    AlertDialog dialog=builder.create();
					    dialog.show();
				}
				return true;
			}
		});
	}
	
	private List<? extends Map<String, ?>> getData(){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 
    	Map<String, Object> map;    	
    	/*ContentResolver resolver = getContentResolver();
    	String order=Media.DEFAULT_SORT_ORDER;
    	 Cursor c = resolver.query(
    			DataBase, 
    			null, null, null, order);
    	c.moveToFirst();
    	do{
    		map = new HashMap<String, Object>();
    		***put value in map***
    		list.add(map);
    	}while(c.moveToNext());*/
    	map = new HashMap<String, Object>();
    	Bitmap card=BitmapFactory.decodeResource(getResources(), R.drawable.card01);
    	map.put("Image", card);
    	map.put("Number","62202011568888");
    	map.put("Name", "Taylor");
    	list.add(map);
    	/*map = new HashMap<String, Object>();
    	card=BitmapFactory.decodeResource(getResources(), R.drawable.card02);
    	map.put("Image", card);
    	map.put("Number","62202011567777");
    	map.put("Name", "Nil");
    	list.add(map);*/
    	map = new HashMap<String, Object>();
    	card=BitmapFactory.decodeResource(getResources(), R.drawable.add);
    	map.put("Image", card);
    	map.put("Number","Tap to add");
    	map.put("Name", "new Card");
    	list.add(list.size(), map);
		return list;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

}
